package com.objects;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Vector;

import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.data.Point3D;
import com.main.Railway;
import com.main.TrainFrame;
import com.sounds.GameSounds;

public class Train extends JDialog implements ActionListener{
	
	Clip engineSound=null;
	Clip train_horn=null;

	public Wagon[] wagons=null;
	
	private JPanel center;
	private JLabel tractionDisplay;
	private JButton tractionPlus;
	private JButton tracionMinus;
	
	private JLabel speedometer;
	
	private Railway railway=null;
	
	double TRACTION_INCREMENT=40;
	
	public double traction=0;

	private JButton tractionStop;

	private JButton playHorn;
	private String trainCode;
	
	public Train(){
		
		engineSound=GameSounds.getClip(GameSounds.SOUNDS_DIRECTORY+"train_marching.wav");
		train_horn=GameSounds.getClip(GameSounds.SOUNDS_DIRECTORY+"train_horn.wav");
		
		
	}
	

	public Object clone() throws CloneNotSupportedException {
	
		
		
		Train tr=new Train();
		
		
		tr.wagons=new Wagon[wagons.length];
		
		for (int i = 0; i < wagons.length; i++) {
			tr.wagons[i]=(Wagon)wagons[i].clone();
		}
		
		tr.trainCode=trainCode;
		tr.playHorn=playHorn;
		
		return tr;
		
	}
	
	
	public Train(String trainCode, int numWagon, Railway railway){

		this();
		
		this.railway=railway;
		
		wagons=new Wagon[numWagon];
		
		for(int i=0;i<wagons.length;i++){

			wagons[i]=new Wagon(railway);	
			
		}					

		buildGUI(trainCode);
		
		setTraction(0);
		
		new Thread(){
			
			public void run() {
				
				while(true){
				
					displaySpeed();
					
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				
				}
			}


			
			
			
		}.start();
		
	}
	
	

	
	private void buildGUI(String trainCode) {
		
		
		TrainFrame tf=(TrainFrame)railway;
		 		
		setTrainCode(trainCode);
		setSize(200,300);
		center=new JPanel(null);
		
		int r=10;	
	
		JLabel jlb=new JLabel("Traction=");
		jlb.setBounds(10,r,70,20);
		center.add(jlb);		
		
		tractionDisplay=new JLabel();
		tractionDisplay.setBounds(80,r,60,20);
		center.add(tractionDisplay);
		
		r+=30;
		
		tractionPlus=new JButton("Push +");
		tractionPlus.addActionListener(this);
		tractionPlus.setBounds(10,r,80,20);
		tractionPlus.addKeyListener(tf);
		center.add(tractionPlus);
		
		tracionMinus=new JButton("Push -");
		tracionMinus.addActionListener(this);
		tracionMinus.setBounds(100,r,80,20);
		tracionMinus.addKeyListener(tf);
		center.add(tracionMinus);
		
		r+=30;
		
		tractionStop=new JButton("Stop");
		tractionStop.addActionListener(this);	
		tractionStop.setBounds(50,r,100,20);
		tractionStop.addKeyListener(tf);
		center.add(tractionStop);
		
		r+=30;
		
		jlb=new JLabel("Speed=");
		jlb.setBounds(10,r,50,20);
		center.add(jlb);
		
		speedometer=new JLabel();
		speedometer.setBounds(70,r,100,20);	
		center.add(speedometer);
		
		
		r+=30;
		
		playHorn=new JButton("Horn");
		playHorn.addActionListener(this);
		playHorn.setBounds(10,r,80,20);
		center.add(playHorn);
		
		add(BorderLayout.CENTER,center);
				
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		Object obj = arg0.getSource();
		
		if(obj==tractionPlus)
			setTraction(this.traction+TRACTION_INCREMENT);
		else if(obj==tractionStop)
			setTraction(0);
		else if(obj==tracionMinus)
			setTraction(this.traction-TRACTION_INCREMENT);
		else if(obj==playHorn)
			playHorn();
		
	}

	private void playHorn() {
		GameSounds.makeSound(train_horn);		
	}




	private void setTraction(double traction_value) {
		
		
		this.traction=traction_value;
		
		if(traction!=0){
			
			if(!engineSound.isRunning())
				GameSounds.makeSoundLoop(engineSound,Clip.LOOP_CONTINUOUSLY);
		        
		}
		else
			GameSounds.stop(engineSound);
		
		tractionDisplay.setText(addZeros(traction_value));
		
	
	}
	
	private String addZeros(double value) {
	
		value=Math.round(value*100.0)/100.0;
		
		DecimalFormat df=new DecimalFormat("####0.00");
		
		return df.format(value);

	}
	
	
	private void displaySpeed() {
		
		
		double speed=Point3D.calculateNorm(wagons[0].vcm);
		speedometer.setText(addZeros(speed));
		
	}


	public String getTrainCode() {
		return trainCode;
	}


	public void setTrainCode(String trainCode) {
		
		this.trainCode = trainCode;
		
		setTitle(trainCode);
	}


	public void addWagon(Wagon w, int pos) {
		
		
		
		Vector vWagons=new Vector();
		
		boolean found=false;
		
		for (int i = 0; i < wagons.length; i++) {
			
			
			if(i==pos){
				vWagons.add(w);
			    found=true;
			}
			vWagons.add(wagons[i]);
			
		}
		
		if(!found){
			
			vWagons.add(w);
		}
	
		wagons = new Wagon[vWagons.size()];
		
		for (int i = 0; i < vWagons.size(); i++) {
			
			wagons[i]=(Wagon) vWagons.elementAt(i);
			
		}
		

		
	}


	public void deleteWagon(int pos) {
		
		Vector vWagons=new Vector();
		
		for (int i = 0; i < wagons.length; i++) {
			
			
			if(i==pos)
				continue;
			
			vWagons.add(wagons[i]);
			
		}
	
		wagons = new Wagon[vWagons.size()];
		
		for (int i = 0; i < vWagons.size(); i++) {
			
			wagons[i]=(Wagon) vWagons.elementAt(i);
			
		}
		
	};

}
